#******************************************************************
#
# Copyright 2015 Samsung Electronics All Rights Reserved.
#
#-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
#-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=

##
# rcs_common (primitiveResource and expiryTimer) build script
##

import os

# SConscript file for Local PKI google tests
gtest_env = SConscript('#extlibs/gtest/SConscript')
lib_env = gtest_env.Clone()

# Add third party libraries
SConscript('#service/third_party_libs.scons', exports = 'lib_env')
rcs_common_env = lib_env.Clone()
target_os = rcs_common_env.get('TARGET_OS')
release = rcs_common_env.get('RELEASE')
src_dir = rcs_common_env.get('SRC_DIR')

######################################################################
# Build flags
######################################################################
rcs_common_env.AppendUnique(CPPPATH=[
    '#/extlibs',
    '#/resource/c_common',
    '#/resource/c_common/oic_malloc/include',
    '#/resource/c_common/oic_string/include',
    '#/resource/include',
    '#/resource/csdk/include',
    '#/resource/csdk/stack/include',
    '#/resource/oc_logger/include',
    '../../include',
    'primitiveResource/include'
])

rcs_common_env.AppendUnique(CPPPATH=['expiryTimer/include', 'expiryTimer/src'])

if target_os not in ['windows']:
    rcs_common_env.AppendUnique(CXXFLAGS=['-std=c++0x', '-Wall'])

if target_os not in ['darwin', 'ios', 'windows']:
    rcs_common_env.AppendUnique(LINKFLAGS=['-Wl,--no-undefined'])
    rcs_common_env.AppendUnique(LIBS=['dl'])

if target_os == 'android':
    rcs_common_env.AppendUnique(CXXFLAGS=['-frtti', '-fexceptions'])
    rcs_common_env.PrependUnique(LIBS=['gnustl_shared', 'log'])
    rcs_common_env.AppendUnique(LINKFLAGS=['-Wl,-soname,librcs_common.so'])

rcs_common_env.AppendUnique(LIBS=['oc'])

if rcs_common_env.get('SECURED') == '1':
    if rcs_common_env.get('WITH_TCP') == True:
        rcs_common_env.AppendUnique(LIBS=['mbedtls', 'mbedx509', 'mbedcrypto'])

######################################################################
# Source files and Targets
######################################################################
TIMER_SRC_DIR = 'expiryTimer/src/'
RESOURCE_SRC = 'primitiveResource/src/'
rcs_common_src = [
    TIMER_SRC_DIR + 'ExpiryTimerImpl.cpp', TIMER_SRC_DIR + 'ExpiryTimer.cpp',
    RESOURCE_SRC + 'PresenceSubscriber.cpp',
    RESOURCE_SRC + 'PrimitiveResource.cpp', RESOURCE_SRC + 'RCSException.cpp',
    RESOURCE_SRC + 'RCSAddress.cpp',
    RESOURCE_SRC + 'RCSResourceAttributes.cpp',
    RESOURCE_SRC + 'RCSRepresentation.cpp'
]

rcs_common_static = rcs_common_env.StaticLibrary('rcs_common', rcs_common_src)
if target_os not in ['ios']:
    rcs_common_shared = rcs_common_env.SharedLibrary('rcs_common',
                                                     rcs_common_src)
    rcs_common_env.InstallTarget([rcs_common_static, rcs_common_shared],
                                 'rcs_common')
    rcs_common_env.UserInstallTargetLib([rcs_common_static, rcs_common_shared],
                                        'rcs_common')
else:
    rcs_common_env.InstallTarget([rcs_common_static], 'rcs_common')
    rcs_common_env.UserInstallTargetLib([rcs_common_static], 'rcs_common')

rcs_common_env.AddPthreadIfNeeded()

######################################################################
# Build Test Expiry Timer and primitive Resource
######################################################################
if target_os in ['linux']:
    SConscript('primitiveResource/unittests/SConscript')
